package com.tsf.demo.tools.controller;

import com.google.gson.JsonObject;
import com.tencent.cloud.task.sdk.core.utils.DateUtil;
import com.tsf.demo.tools.service.DiscoveryClientSerivce;
import com.tsf.demo.tools.service.Register;
import org.jose4j.json.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

//tangfuzhong
@RestController
public class CommonController {

	@Autowired
	private DiscoveryClient discoveryClient;
	@Autowired
	private DiscoveryClientSerivce discoveryClientSerivce;

	private Logger logger = LoggerFactory.getLogger(this.getClass());


	//尝试查询当前实例的服务列表
	@RequestMapping(value = "/echo-serverList", method = RequestMethod.GET)
	public List<Object> getServerList() {
		return getServerList(discoveryClient);
	}

	@RequestMapping(value = "/echo-setInfo", method = RequestMethod.GET)
	public List<Object> setInfo(@RequestParam(value="test_name",required=true) String test_name) {
		Register.mapServerList.clear();
		Register.testName = test_name;
		discoveryClientSerivce.watchRegister();
		List list = new ArrayList();
		list.add(DateUtil.getDateStr(System.currentTimeMillis()));
		list.add(DateUtil.getCurrentDateStr());
		list.add("Register.testName="+Register.testName);
		list.add("DiscoveryClientSerivce.count"+DiscoveryClientSerivce.count);
		list.addAll(Register.listLog);
		return list;
	}

	@RequestMapping(value = "/echo-registerInfo", method = RequestMethod.GET)
	public List<Object> getRegisterInfo() {

		List list = new ArrayList();

		try
		{
			list.add(DateUtil.getDateStr(System.currentTimeMillis()));
			list.add(DateUtil.getCurrentDateStr());
			list.add("DiscoveryClientSerivce.count"+DiscoveryClientSerivce.count);
			list.addAll(Register.listLog);
			list.addAll(Register.listMessage);

			int size = Register.mapServerList.size();
			list.add("注册服务数量:"+ size);

			long useTime = 0;
			if (size>0)
			{
				for(Map.Entry<String, Long> entry:Register.mapServerList.entrySet()){
					useTime = useTime + entry.getValue();
				}

				BigDecimal bigDecimal = new BigDecimal(useTime);
				BigDecimal bigDecimalSize = new BigDecimal(size);
				list.add("all useTime: "+ useTime +" , " + bigDecimalSize);
				long avgTime = bigDecimal.divide(bigDecimalSize,2,BigDecimal.ROUND_HALF_UP).longValue();

				list.add("平均延时: "+ avgTime +" ms");
			}

			return list;
		} catch (Exception e)
		{
		  list.add(e);
		  return list;
		}
	}



	public static List<Object> getServerList(DiscoveryClient discoveryClient) {
		List<Object> listAll = new ArrayList<Object>();
		List<String> list = discoveryClient.getServices();
		listAll.add(list);//显示所有serviceId
		for (String serviceId:list) {
			List<ServiceInstance> listService = discoveryClient.getInstances(serviceId);
			listAll.addAll(listService);
		}

		String discoveryClientInfo = "discoveryClient=" + discoveryClient.getClass().getName();
		listAll.add(discoveryClientInfo);
		return listAll;
	}

}